<template>
  <view class="container">
  <uni-card>
    <view>登录跳转测试</view>
    <uni-forms :modelValue="formData" :rules="formRules" ref="loginForm">
			<uni-forms-item label="用户名" name="name">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
			</uni-forms-item>
		</uni-forms>
    <uni-row>
      <uni-col :span="12"><button @click="goback">返回</button></uni-col>
      <uni-col :span="12"><button @click="submitForm">登录</button></uni-col>
    </uni-row>

  </uni-card>
  </view>
</template>

<script lang="ts">
import { MyPage } from '@/my/super'
import {Action} from '@gowiny/vue-class'

class LoginPage extends MyPage{
  formData={
    name:''
  }
  formRules =  {
	// 对name字段进行必填验证
	name: {
		// name 字段的校验规则
		rules:[
			// 校验 name 不能为空
			{
				required: true,
				errorMessage: '请填写用户名',
			},
			// 对name字段进行长度验证
			{
				minLength: 3,
				maxLength: 6,
				errorMessage: '{label}长度在 {minLength} 到 {maxLength} 个字符',
			}
		],
		validateTrigger:'submit'
	}
}
  @Action("user/setLoginResult")
    setLoginResult!:(loginResult:any)=>void
  goback(){
    uni.navigateBack({
      delta:1
    })
  }
  async submitForm(){
    const loginForm:any = this.$refs.loginForm
    try{
      const result = await loginForm.validate()
      this.setLoginResult({
        success:true,
        userInfo:{
          username:result.name
        }
      })

      const url = this.$Route.query?.url || '/pages/index/index'
      uni.navigateTo({
        url:url
      })
    }catch(err){
      console.log('表单错误信息',err)
    }
  }
}
export default LoginPage
</script>

<style>
</style>
